package org.ymh.unix_file_system.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lenovo
 * @version 1.0
 * @description: TODO
 * @date 2023/12/17 0:09
 */
@Component
public class FileSystemError {
    @Autowired
    private GlobalVariable globalVariable;
    public  final Integer INodeIndexOutOfBoundsError=0;
    public  final Integer OutOfMemoryError=1;
    public  final Integer BlockIndexOutOfBoundsError =2;
    public final Integer BlockOfFreeBlockLinkedListOccupyError=3;
    public final Integer OutOfSingleFileMaxSizeError=4;//文件最大范围
    public final Integer SameFileNameError=5;//相同文件名
    public final Integer DeleteDirNodeHaveItemError=6;//试图删除非空目录
    public final Integer HaveNoRestInodeError=7;//inode节点分配殆尽

    public boolean isOutIndex(int index,int MaxIndex,Integer error)
    {
        if(index<0||index>=MaxIndex)
        {
            globalVariable.setError(GetError(error));
            return true;
        }
        return false;
    }
    public boolean isOutMemory(Long B,Integer error)
    {
        int kb = (int) Math.ceil(B*1.0 / 1024);
        if(kb>globalVariable.superBlock.blockCountRest)
        {
            globalVariable.setError(GetError(error));
            return true;
        }
        return false;
    }
    public void setError(Integer error)
    {
        globalVariable.setError(GetError(error));
    }
    public String GetError(Integer error)
    {
        String errorStr=null;
        if(error==INodeIndexOutOfBoundsError)
            errorStr= "INodeIndexOutOfBoundsError";
        else if(error==OutOfMemoryError)
            errorStr="OutOfMemoryError";
        else if(error== BlockIndexOutOfBoundsError)
            errorStr="BlockIndexOutOfBounds";
        else if(error==BlockOfFreeBlockLinkedListOccupyError)
            errorStr="BlockOfFreeBlockLinkedListOccupyError";
        else if(error==OutOfSingleFileMaxSizeError)
            errorStr="OutOfSingleFileMaxSizeError";
        else if(error==SameFileNameError)
            errorStr="SameFileNameError";
        else if(error==DeleteDirNodeHaveItemError)
            errorStr="DeleteDirNodeHaveItemError";
        else if(error==HaveNoRestInodeError)
            errorStr="HaveNoRestInodeError";
        if(errorStr!=null) {
            System.err.println(errorStr);
            Exception e=new Exception();//自定义输出异常
            e.printStackTrace();
        }
        return errorStr;
    }



}
